﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;

using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebControls;

namespace zevenseas.Community.Web.UI.WebControls
{
    public class zsBoundUserField : SPBoundField
    {
        protected override void ChildControlDataBinding(Control childControl, object dataItem, MemberDescriptor dataFieldPropertyDescriptor)
        {
            Literal fieldValue = new Literal();

            string propertyValue = GetPropertyValue(dataItem, dataFieldPropertyDescriptor.Name);

            SPFieldUserValue gridUserValue = new SPFieldUserValue(SPContext.Current.Web, propertyValue);
            if (!string.IsNullOrEmpty(gridUserValue.ToString()))
            {
                fieldValue.Text = gridUserValue.User.Name.ToString();
            }

            PlaceHolder placeHolder = (PlaceHolder)childControl;
            placeHolder.Controls.Add(fieldValue);
        }

        protected override Control GetChildControlInstance()
        {
            return new PlaceHolder();
        }
    }

    public class zsBoundHTMLField : SPBoundField
    {
        protected override void ChildControlDataBinding(Control childControl, object dataItem,
                                                        MemberDescriptor dataFieldPropertyDescriptor)
        {
            PlaceHolder placeHolder = (PlaceHolder)childControl;
            string propertyValueAsHtml = GetPropertyValueAsHtml(dataItem, dataFieldPropertyDescriptor.Name);
            if (!string.IsNullOrEmpty(propertyValueAsHtml))
            {
                Label label = new Label();
                label.Text = SPHttpUtility.HtmlDecode(propertyValueAsHtml);
                placeHolder.Controls.Add(label);
            }
        }
        protected override Control GetChildControlInstance()
        {
            return new PlaceHolder();
        }
    }

    public class zsBoundDateTimeField : SPBoundField
    {
        private readonly CultureInfo _culture;
        private string _dateFormat = "d";

        public zsBoundDateTimeField()
        {
            SPRegionalSettings regionalSettings = SPContext.Current.Web.CurrentUser.RegionalSettings ??
                                                  SPContext.Current.Web.RegionalSettings;

            if (regionalSettings != null)
            {
                _culture = new CultureInfo((int)regionalSettings.LocaleId);
            }
            else
            {
                _culture = CultureInfo.CurrentUICulture;
            }
        }
        public string DateFormat
        {
            get { return _dateFormat; }
            set
            {
                if (string.IsNullOrEmpty(value)) throw new ArgumentNullException("value");
                _dateFormat = value;
            }
        }
        protected override void ChildControlDataBinding(Control childControl, object dataItem,
                                                   MemberDescriptor dataFieldPropertyDescriptor)
        {
            string value = GetPropertyValue(dataItem, dataFieldPropertyDescriptor.Name); Label label = (Label)childControl; DateTime date;
            if (DateTime.TryParse(value, out date))
            {
                label.Text = date.ToString(DateFormat, _culture);
            }
            else
            {
                label.Text = value;
            }
        }
        protected override Control GetChildControlInstance()
        {
            return new Label();
        }
    }

    public class zsBoundLookupField : SPBoundField
    {
        protected override void ChildControlDataBinding(Control childControl, object dataItem, MemberDescriptor dataFieldPropertyDescriptor)
        {
            string value = GetPropertyValue(dataItem, dataFieldPropertyDescriptor.Name);
            Label label = (Label)childControl;

            SPFieldLookupValue multivalue = new SPFieldLookupValue(value);
            label.Text = multivalue.LookupValue;

        }
        protected override Control GetChildControlInstance()
        {
            return new Label();
        }
    }

    public class zsBoundChoiceField : SPBoundField
    {
        protected override void ChildControlDataBinding(Control childControl, object dataItem, MemberDescriptor dataFieldPropertyDescriptor)
        {
            string value = GetPropertyValue(dataItem, dataFieldPropertyDescriptor.Name);
            Label label = (Label)childControl;
            label.Text = value.ToString();

        }
        protected override Control GetChildControlInstance()
        {
            return new Label();
        }
    }

    //public class zsBoundUrlField : SPBoundField
    //{
    //    protected override void ChildControlDataBinding(Control childControl, object dataItem, MemberDescriptor dataFieldPropertyDescriptor)
    //    {
    //        string propertyValueAsHtml = GetPropertyValueAsHtml(dataItem, dataFieldPropertyDescriptor.Name);
    //        SPFieldUrlValue GridUrlValue = new SPFieldUrlValue(propertyValueAsHtml);

    //        HyperLink GridUrlField = new HyperLink();

    //        GridUrlField.Text = GridUrlValue.Description;
    //        GridUrlField.NavigateUrl = GridUrlValue.Url;

    //        PlaceHolder placeHolder = (PlaceHolder)childControl;
    //        placeHolder.Controls.Add(GridUrlField);
    //    }

    //    protected override Control GetChildControlInstance()
    //    {
    //        return new PlaceHolder();
    //    }
    //}

    public class zsBoundCalculatedField : SPBoundField
    {
        protected override void ChildControlDataBinding(Control childControl, object dataItem, MemberDescriptor dataFieldPropertyDescriptor)
        {
            string propertyValueAsHtml = GetPropertyValueAsHtml(dataItem, dataFieldPropertyDescriptor.Name);
            propertyValueAsHtml = propertyValueAsHtml.Remove(0, (propertyValueAsHtml.IndexOf("#") + 1));

            Label label = (Label)childControl;
            label.Text = propertyValueAsHtml.ToString();

        }
        protected override Control GetChildControlInstance()
        {
            return new Label();
        }
    }
}
